﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;
using System.Windows.Markup;
using System.Xml.Serialization;
using System.IO;
using System.Windows;
using SymbolEditor.WpfLibrary.Helper;

namespace SymbolEditor.WpfLibrary
{
    class GraphicGenerator
    {
        private System.Windows.ResourceDictionary resourceDictionary;

        private List<Graphic> graphics = new List<Graphic>();
        public Dictionary<string, string> XamlSymbols = null;



        public GraphicGenerator(System.Windows.ResourceDictionary resourceDictionary)
        {
            // TODO: Complete member initialization
            this.resourceDictionary = resourceDictionary;
        }


        internal void ViewInGraphicLayer(ESRI.ArcGIS.Client.GraphicsLayer graphicsLayer)
        {            
            foreach (Graphic graphic in graphics)
            {
                graphicsLayer.Graphics.Add(graphic);
            }            
        }

        internal void GeneratorGraphics(ESRI.ArcGIS.Client.Geometry.Envelope envelope,string setname)
        {
            int colNum = getColumnMum();
            int rowNum = (int)Math.Ceiling((double)resourceDictionary.Count / colNum);
            double xmin = envelope.XMin;
            double ymax = envelope.YMax;
            double geowidth = envelope.Width;
            double elemntwidth = geowidth / (colNum * 2 + 1);
            double elementheight = elemntwidth;
            double orgcntY = ymax + elementheight / 2;
            double orgcntX = xmin - elemntwidth / 2;
            int t = 0;
            BasicGeometryFactory geomFactory = new BasicGeometryFactory();

            foreach (object key in resourceDictionary.Keys)
            {
                object obj=resourceDictionary[key];
                Symbol symbol = obj as Symbol;
                if (symbol == null)
                    continue;
                int row = (int)t / colNum;
                int col = (int)Math.IEEERemainder(t, colNum);
                double cntX = orgcntX + 2 * elemntwidth * col;
                double cntY = orgcntY - 2 * elementheight * row;
                ESRI.ArcGIS.Client.Geometry.Envelope elementEntent = new ESRI.ArcGIS.Client.Geometry.Envelope();
                elementEntent.XMin = cntX - elemntwidth / 2;
                elementEntent.XMax = cntX + elemntwidth / 2;
                elementEntent.YMin = cntY - elementheight / 2;
                elementEntent.YMax = cntY + elementheight / 2;
                GeometryType geometryType = getSymbolType(symbol);
                Geometry geom = geomFactory.ProductGeometry(geometryType, elementEntent);
                if (geom != null)
                {
                    Graphic graphic = new Graphic()
                    {
                        Geometry = geom,
                        Symbol = symbol
                    };

                    graphic.Attributes.Add(ArcServerClientHelper.STR_GRAPHIC_ATT_KEY, key.ToString());
                    graphic.Attributes.Add(ArcServerClientHelper.STR_GRAPHIC_ATT_SET, setname);
                    //symbol.ControlTemplate = null;
                    //XmlSerializer serializer = new XmlSerializer(symbol.GetType());
                    //XamlWriter.Save()
                    ResourceDictionary rd = new ResourceDictionary();
                    rd.Add(key.ToString(), symbol);
                    string xamltext = string.Empty;
                    try
                    {
                        xamltext = XamlSymbols[key.ToString()];
                    }
                    catch { xamltext = XamlWriter.Save(rd); }

                    graphic.Attributes.Add(ArcServerClientHelper.STR_GRAPHIC_ATT_XAML, xamltext);
                    
                    graphics.Add(graphic);

                    t++;
                }
                else
                {
                    t--;
                }
            }

                
        }

        private GeometryType getSymbolType(Symbol symbol)
        {
            if (symbol is LineSymbol)
                return GeometryType.Polyline;
            if (symbol is FillSymbol)
                return GeometryType.Polygon;
            if (symbol is MarkerSymbol)
                return GeometryType.Point;
            return GeometryType.Point;
        }

        private int getColumnMum()
        {
            return 10;
        }
    }
}
